package com.manger.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageUtils {

    public static void cutHalf(String resource,String target) throws Exception{
        BufferedImage sourceImage = ImageIO.read(new File(resource));
        // 创建一个新的Rectangle对象，用于表示裁剪区域的位置和大小
        Rectangle rect = new Rectangle(0, 0, sourceImage.getWidth(), sourceImage.getHeight()/2);
        // 裁剪图像
        BufferedImage croppedImage = sourceImage.getSubimage(rect.x, rect.y, rect.width, rect.height);
        // 保存裁剪后的图像
        ImageIO.write(croppedImage, "jpg", new File(target));
    }

    public static void main(String[] args) throws Exception{
        ImageUtils.cutHalf("C:\\Users\\liuhj\\Desktop\\test\\1.jpg", "C:\\Users\\liuhj\\Desktop\\test\\2.jpg");
    }
}
